var fs = require('fs');
var path = require('path');
var pidfile = path.join(__dirname, 'app.pid');
console.info('process.pid===' + process.pid);
fs.writeFileSync(pidfile, process.pid);
var Keyboard = require('./keyboard.js');
var k = new Keyboard('event4');
var write = function (obj) {
    fs.appendFile('./kb.log', obj.keyId, 'utf8', function (err) {
        fs.writeFile('./error.log', err)
    });

}
k.on('keyup', write);
k.on('keydown', write);
//k.on('keypress', console.log);

k.on('error', console.error); // Something wrent wrong, keyboard disconnected or something


process.stdin.resume();
process.on('SIGTERM', function () {
    console.info('bye1');
    if (fs.existsSync(pidfile)) {
        fs.unlinkSync(pidfile);
    }
    process.exit(0);
});

process.on('SIGINT', function () {
    console.info('bye2');
    if (fs.existsSync(pidfile)) {
        fs.unlinkSync(pidfile);
    }
    process.exit(0);
});

console.info('bye');